package com.carey.blog.event.tencent.microblog;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.json.JSONObject;

import com.carey.blog.event.EventTypes;
import com.carey.blog.framework.latke.event.AbstractEventListener;
import com.carey.blog.framework.latke.event.Event;
import com.carey.blog.framework.latke.event.EventException;
import com.carey.blog.framework.latke.event.EventManager;
import com.carey.blog.libs.qweibo.QWeiboSyncApi;
import com.carey.blog.libs.qweibo.QWeiboType.ResultType;
import com.carey.blog.model.Article;
import com.carey.blog.model.Preference;
import com.carey.blog.util.Preferences;

/**
 * This listener is responsible for sending article publish information to
 * author's <a href="http://t.qq.com">Tencent microblog</a>.
 * 
 */
public final class TencentMicroblogSender extends
		AbstractEventListener<JSONObject> {

	/**
	 * Logger.
	 */
	private static final Logger LOGGER = Logger
			.getLogger(TencentMicroblogSender.class.getName());
	/**
	 * Preference utilities.
	 */
	private Preferences preferenceUtils = Preferences.getInstance();

	/**
	 * Constructs a {@link TencentMicroblogSender} object with the specified
	 * event manager.
	 * 
	 * @param eventManager
	 *            the specified event manager
	 */
	public TencentMicroblogSender(final EventManager eventManager) {
		super(eventManager);
	}

	@Override
	public void action(final Event<JSONObject> event) throws EventException {
		final JSONObject preference = preferenceUtils.getPreference();
		if (null == preference) {
			throw new EventException("Not found preference");
		}

		final boolean enabled = preference
				.optBoolean(Preference.ENABLE_POST_TO_TENCENT_MICROBLOG);
		if (!enabled) {
			return;
		}

		final JSONObject data = event.getData();
		LOGGER.log(
				Level.FINER,
				"Processing an event[type={0}, data={1}] in listener[className={2}]",
				new Object[] { event.getType(), data,
						TencentMicroblogSender.class.getName() });
		try {
			final JSONObject article = data.getJSONObject(Article.ARTICLE);

			final QWeiboSyncApi api = new QWeiboSyncApi();
			final String appKey = preference
					.getString(Preference.TENCENT_MICROBLOG_APP_KEY);
			final String appSecret = preference
					.getString(Preference.TENCENT_MICROBLOG_APP_SECRET);
			final String tokenKey = preference
					.getString(Preference.TENCENT_MICROBLOG_TOKEN_KEY);
			final String tokenSecret = preference
					.getString(Preference.TENCENT_MICROBLOG_TOKEN_SECRET);

			final String articleTitle = article
					.getString(Article.ARTICLE_TITLE);
			final String blogTitle = preference
					.getString(Preference.BLOG_TITLE);
			final String blogHost = preference.getString(Preference.BLOG_HOST);
			final String articlePermalink = "http://" + blogHost
					+ article.getString(Article.ARTICLE_PERMALINK);

			final StringBuilder contentBuilder = new StringBuilder(articleTitle);
			contentBuilder.append(" - ").append(blogTitle).append(" ")
					.append(articlePermalink);

			api.publishMsg(appKey, appSecret, tokenKey, tokenSecret,
					contentBuilder.toString(), null, ResultType.ResultType_Json);
		} catch (final Exception e) {
			LOGGER.log(Level.SEVERE,
					"Sends article to Tencent microblog error: {0}",
					e.getMessage());
		}
	}

	/**
	 * Gets the event type {@linkplain EventTypes#ADD_ARTICLE}.
	 * 
	 * @return event type
	 */
	@Override
	public String getEventType() {
		return EventTypes.ADD_ARTICLE;
	}
}
